
export default function useCountDown() {
    //1.准备一个用于实现倒计时的秒数状态
    const count = ref(60)
    //2.实现倒计时的核心逻辑
    const { pause, resume, isActive } = useIntervalFn(() => {
        //如果秒数大于0
        if (count.value > 0) {
            //r让秒数自己减1
            count.value--
        } else {
            //如果count小于等于0停止倒计时
            pause()
        }
    }, 1000, {
        immediate: false
    })

    //外部通过调用start方法开启计时
    const start = (seconds: number) => {
        //检测定时器是否正在运行
        if (isActive.value) return;
        //重置计时时间
        count.value = seconds
        //开始倒计时
        resume()
    }
    return { count, start, isActive }
}